﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace ImryStefanGAnamespace
{

    class Program
    {

        public static double totalTime = 5*60*12; // in seconds

        public static double maxPopulation = 1000;
                
        // Global variables, for convenience

        public static Random rand;

        public static CVRPData vrpData;
        
        static void Main(string[] args)
        {
                Console.WriteLine("Welcome to our Genetic Algorithm implementation");

                Stopwatch stopWatch = new Stopwatch();

                stopWatch.Start();

                rand = new Random();

                // If there are command line arguments, the first one is the file name
                String inputFileName;
                if (args.Length != 0)
                    inputFileName = args[0];
                else
                {
                    //inputFileName = "Christofides_06.vrp";
                    //inputFileName = "Example1.vrp";
                    //inputFileName = "Example2.vrp";
                    inputFileName = "Golden_09.vrp";
                    //inputFileName = "myTrial.vrp";
                    //inputFileName = "myTrial2.vrp";
                    //inputFileName = "myTrial3.vrp";
                    //inputFileName = "myTrial4.vrp";
                    //inputFileName = "B-n31-k5.vrp";
                    //inputFileName = "B-n34-k5.vrp";
                    //inputFileName = "Taillard_100C.vrp";
                }

                // Create the parser object, and read in all the data from file. This object can read ANY TSPLIB file. Code borrowed from HeuristicLab.
                Console.WriteLine("Data file: " + inputFileName);
                TSPLIBParser myParser = new TSPLIBParser(inputFileName);
                myParser.Parse();

                // Create the global vrp data object, using the parsed data
                Program.vrpData = new CVRPData(myParser);

                // Run the metaheuristic
                CVRPSolution mySolution = GeneticAlgorithmClass.GeneticAlgorithm(stopWatch);
                double totalCostFinal = mySolution.TotalCost();
                int numRoutesFinal = mySolution.routesInSolution.Count;

                // Check if solution makes sense
                Console.WriteLine("Final solution:");
                mySolution.FeasibilityCheck();

                // Calculate the total time
                stopWatch.Stop();
                TimeSpan ts = stopWatch.Elapsed;

                // Output the solution to file
                //mySolution.WriteToFile(inputFileName + ".opt", ts);

                // Output the statistics to csv
                string teamName = "ImryStefan";
                string outputFile = teamName + ".csv";
                using (StreamWriter writer = new StreamWriter(outputFile, true))
                {
                    writer.WriteLine(teamName + ", " + inputFileName + ", " + totalCostFinal + ", " + numRoutesFinal);
                    writer.Close();
                }

                //Console.ReadKey();
        }


    }
}
